package com.hzhc.utils;

import com.hzhc.enumBean.ResultUtilCodeEnum;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
import org.apache.commons.compress.archivers.zip.ZipArchiveInputStream;
import org.apache.poi.ss.formula.functions.T;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;

/**
 * @author wuxiaoke
 * @description rar解压工具类
 * @date 2024/6/13 上午10:32
 */
@Log4j2
public class ZipUtils {

    /**
     * rar文件解压
     *
     * @param zipFile  rar压缩包
     * @param destPath 解压保存路径
     */
    public static ResultUtil<T> unPackZip(File zipFile, String destPath) throws IOException {

        InputStream zipInputStream = Files.newInputStream(zipFile.toPath());
        ZipArchiveInputStream zip = new ZipArchiveInputStream(zipInputStream);
        ZipArchiveEntry zipEntry = null;
        List<File> fileList = new ArrayList<>();
        boolean isCntrFile = false;
        while ((zipEntry = zip.getNextEntry()) != null) {
            String fileName_zip = zipEntry.getName();
            if(fileName_zip.startsWith("__MACOSX/")){
                continue;
            }
            if(zipEntry.isDirectory()){
               // 是文件夹
                isCntrFile = CheckUtils.checkCntrNo(zipEntry.getName());
                File file = new File(destPath + File.separator + fileName_zip);
                file.mkdirs();
            }else{
                //是文件
                //判断是否包含箱号文件夹
//                String splitFlag=File.separator;
//                if(!"/".equals(splitFlag)){
//                    splitFlag="\\\\";
//                }
                String[] fileName_arr =  zipEntry.getName().split("/");
                for (String fileName : fileName_arr) {
                    isCntrFile = CheckUtils.checkCntrNo(fileName);
                    if(isCntrFile){
                        break;
                    }
                }
                if(!isCntrFile){
                    //说明改文件的目录不包含箱号文件夹，不进行解压
                    continue;
                }
                //解压文件到临时文件夹
                File file = new File(destPath + File.separator + fileName_zip.trim());
                if (!file.exists()) {
                    if (!file.getParentFile().exists()) {
                        // 相对路径可能多级，可能需要创建父目录.
                        file.getParentFile().mkdirs();
                    }
                    file.createNewFile();
                }
                BufferedOutputStream out = new BufferedOutputStream(Files.newOutputStream(file.toPath()));
                byte[] byte_s = new byte[1024];
                int num;
                while ((num = zip.read(byte_s, 0, byte_s.length)) > 0) {
                    out.write(byte_s, 0, num);
                }
                out.close();
                fileList.add(file);
            }
        }
        if(fileList.isEmpty()){
            log.info("解压包文件内容无法解析");
            return ResultUtil.build(ResultUtilCodeEnum.ZIPFILE_UNABLE_TO_PARSE);
        }else{
            log.info("解压完成，共找到{}个文件",fileList.size());
            return ResultUtil.ok();
        }
    }

    public static boolean checkCntrNo(String cntrNo){
        if (cntrNo == null || cntrNo.trim().length() != 11) {
            return false;
        }
        if(!Pattern.compile("[A-Z]*").matcher(cntrNo.substring(0,4)).matches()){
            return false;
        }
        return Pattern.matches("\\d+", cntrNo.substring(4, 10));
    }

}
